
def func(n):
    for i in range(n):
        print("开始生成")
        yield i
        print("完成一次")

if __name__ == '__main__':
    f = func(5)
    try:
        print(next(f))
        print(next(f))
        print(next(f))
        print(next(f))
    except StopIteration as stop:
        print("生成结束，抛出StopIteration异常")
